let ApiError = require('../app/error/ApiError');

function response_formatter(ctx){


	if(ctx.body){

		ctx.body = {code:0,message:'success',data:ctx.body};

	}else{

		ctx.body = {code:0,message:'success'};
	}

}


function url_filter(pattern){

	return function* (next){

		let reg = new RegExp(pattern);

		console.log(this);

		try{

			yield next;

		}catch(error){


			if(error instanceof ApiError && reg.test(this.originalUrl)){

				this.status = 200;

				this.body = {

					code:error.code,
					message:error.message

				}
			}

			throw error;

		}

		console.log(this.body)

		
		if(reg.test(this.originalUrl)){

		 	response_formatter(this);
		}

	}
}


module.exports = url_filter;